using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;

namespace LMSData
{
	/// <summary>
	/// Strongly-typed collection for the Thread class.
	/// </summary>
	[Serializable]
	public partial class ThreadCollection : ActiveList<Thread, ThreadCollection> 
	{	   
		public ThreadCollection() {}

	}

	/// <summary>
	/// This is an ActiveRecord class which wraps the Thread table.
	/// </summary>
	[Serializable]
	public partial class Thread : ActiveRecord<Thread>
	{
		#region .ctors and Default Settings
		
		public Thread()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}

		
		private void InitSetDefaults() { SetDefaults(); }

		
		public Thread(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}

		public Thread(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}

		 
		public Thread(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}

		
		protected static void SetSQLProps() { GetTableSchema(); }

		
		#endregion
		
		#region Schema and Query Accessor
		public static Query CreateQuery() { return new Query(Schema); }

		
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}

		}

		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("Thread", TableType.Table, DataService.GetInstance("LMS"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarThreadID = new TableSchema.TableColumn(schema);
				colvarThreadID.ColumnName = "ThreadID";
				colvarThreadID.DataType = DbType.Int32;
				colvarThreadID.MaxLength = 0;
				colvarThreadID.AutoIncrement = true;
				colvarThreadID.IsNullable = false;
				colvarThreadID.IsPrimaryKey = true;
				colvarThreadID.IsForeignKey = false;
				colvarThreadID.IsReadOnly = false;
				colvarThreadID.DefaultSetting = @"";
				colvarThreadID.ForeignKeyTableName = "";
				schema.Columns.Add(colvarThreadID);
				
				TableSchema.TableColumn colvarForumID = new TableSchema.TableColumn(schema);
				colvarForumID.ColumnName = "ForumID";
				colvarForumID.DataType = DbType.Int32;
				colvarForumID.MaxLength = 0;
				colvarForumID.AutoIncrement = false;
				colvarForumID.IsNullable = false;
				colvarForumID.IsPrimaryKey = false;
				colvarForumID.IsForeignKey = true;
				colvarForumID.IsReadOnly = false;
				colvarForumID.DefaultSetting = @"";
				
					colvarForumID.ForeignKeyTableName = "Forum";
				schema.Columns.Add(colvarForumID);
				
				TableSchema.TableColumn colvarParentID = new TableSchema.TableColumn(schema);
				colvarParentID.ColumnName = "ParentID";
				colvarParentID.DataType = DbType.Int32;
				colvarParentID.MaxLength = 0;
				colvarParentID.AutoIncrement = false;
				colvarParentID.IsNullable = true;
				colvarParentID.IsPrimaryKey = false;
				colvarParentID.IsForeignKey = true;
				colvarParentID.IsReadOnly = false;
				colvarParentID.DefaultSetting = @"";
				
					colvarParentID.ForeignKeyTableName = "Thread";
				schema.Columns.Add(colvarParentID);
				
				TableSchema.TableColumn colvarThreadName = new TableSchema.TableColumn(schema);
				colvarThreadName.ColumnName = "ThreadName";
				colvarThreadName.DataType = DbType.String;
				colvarThreadName.MaxLength = 250;
				colvarThreadName.AutoIncrement = false;
				colvarThreadName.IsNullable = true;
				colvarThreadName.IsPrimaryKey = false;
				colvarThreadName.IsForeignKey = false;
				colvarThreadName.IsReadOnly = false;
				colvarThreadName.DefaultSetting = @"";
				colvarThreadName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarThreadName);
				
				TableSchema.TableColumn colvarUserID = new TableSchema.TableColumn(schema);
				colvarUserID.ColumnName = "UserID";
				colvarUserID.DataType = DbType.Int32;
				colvarUserID.MaxLength = 0;
				colvarUserID.AutoIncrement = false;
				colvarUserID.IsNullable = false;
				colvarUserID.IsPrimaryKey = false;
				colvarUserID.IsForeignKey = true;
				colvarUserID.IsReadOnly = false;
				colvarUserID.DefaultSetting = @"";
				
					colvarUserID.ForeignKeyTableName = "User";
				schema.Columns.Add(colvarUserID);
				
				TableSchema.TableColumn colvarCreatedOn = new TableSchema.TableColumn(schema);
				colvarCreatedOn.ColumnName = "CreatedOn";
				colvarCreatedOn.DataType = DbType.DateTime;
				colvarCreatedOn.MaxLength = 0;
				colvarCreatedOn.AutoIncrement = false;
				colvarCreatedOn.IsNullable = true;
				colvarCreatedOn.IsPrimaryKey = false;
				colvarCreatedOn.IsForeignKey = false;
				colvarCreatedOn.IsReadOnly = false;
				colvarCreatedOn.DefaultSetting = @"";
				colvarCreatedOn.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCreatedOn);
				
				TableSchema.TableColumn colvarIsDeleted = new TableSchema.TableColumn(schema);
				colvarIsDeleted.ColumnName = "IsDeleted";
				colvarIsDeleted.DataType = DbType.Boolean;
				colvarIsDeleted.MaxLength = 0;
				colvarIsDeleted.AutoIncrement = false;
				colvarIsDeleted.IsNullable = true;
				colvarIsDeleted.IsPrimaryKey = false;
				colvarIsDeleted.IsForeignKey = false;
				colvarIsDeleted.IsReadOnly = false;
				colvarIsDeleted.DefaultSetting = @"";
				colvarIsDeleted.ForeignKeyTableName = "";
				schema.Columns.Add(colvarIsDeleted);
				
				TableSchema.TableColumn colvarDepth = new TableSchema.TableColumn(schema);
				colvarDepth.ColumnName = "Depth";
				colvarDepth.DataType = DbType.Int32;
				colvarDepth.MaxLength = 0;
				colvarDepth.AutoIncrement = false;
				colvarDepth.IsNullable = true;
				colvarDepth.IsPrimaryKey = false;
				colvarDepth.IsForeignKey = false;
				colvarDepth.IsReadOnly = false;
				colvarDepth.DefaultSetting = @"";
				colvarDepth.ForeignKeyTableName = "";
				schema.Columns.Add(colvarDepth);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["LMS"].AddSchema("Thread",schema);
			}

		}

		#endregion
		
		#region Props
		
		  
		[XmlAttribute("ThreadID")]
		public int ThreadID 
		{
			get { return GetColumnValue<int>("ThreadID"); }

			set { SetColumnValue("ThreadID", value); }

		}

		  
		[XmlAttribute("ForumID")]
		public int ForumID 
		{
			get { return GetColumnValue<int>("ForumID"); }

			set { SetColumnValue("ForumID", value); }

		}

		  
		[XmlAttribute("ParentID")]
		public int? ParentID 
		{
			get { return GetColumnValue<int?>("ParentID"); }

			set { SetColumnValue("ParentID", value); }

		}

		  
		[XmlAttribute("ThreadName")]
		public string ThreadName 
		{
			get { return GetColumnValue<string>("ThreadName"); }

			set { SetColumnValue("ThreadName", value); }

		}

		  
		[XmlAttribute("UserID")]
		public int UserID 
		{
			get { return GetColumnValue<int>("UserID"); }

			set { SetColumnValue("UserID", value); }

		}

		  
		[XmlAttribute("CreatedOn")]
		public DateTime? CreatedOn 
		{
			get { return GetColumnValue<DateTime?>("CreatedOn"); }

			set { SetColumnValue("CreatedOn", value); }

		}

		  
		[XmlAttribute("IsDeleted")]
		public bool? IsDeleted 
		{
			get { return GetColumnValue<bool?>("IsDeleted"); }

			set { SetColumnValue("IsDeleted", value); }

		}

		  
		[XmlAttribute("Depth")]
		public int? Depth 
		{
			get { return GetColumnValue<int?>("Depth"); }

			set { SetColumnValue("Depth", value); }

		}

		
		#endregion
		
		
		#region PrimaryKey Methods
		
		public LMSData.MessageCollection MessageRecords()
		{
			return new LMSData.MessageCollection().Where(Message.Columns.ThreadID, ThreadID).Load();
		}

		public LMSData.ThreadCollection ChildThreadRecords()
		{
			return new LMSData.ThreadCollection().Where(Thread.Columns.ParentID, ThreadID).Load();
		}

		#endregion
		
			
		
		#region ForeignKey Properties
		
		/// <summary>
		/// Returns a Forum ActiveRecord object related to this Thread
		/// 
		/// </summary>
		public LMSData.Forum Forum
		{
			get { return LMSData.Forum.FetchByID(this.ForumID); }

			set { SetColumnValue("ForumID", value.ForumID); }

		}

		
		
		/// <summary>
		/// Returns a Thread ActiveRecord object related to this Thread
		/// 
		/// </summary>
		public LMSData.Thread ParentThread
		{
			get { return LMSData.Thread.FetchByID(this.ParentID); }

			set { SetColumnValue("ParentID", value.ThreadID); }

		}

		
		
		/// <summary>
		/// Returns a User ActiveRecord object related to this Thread
		/// 
		/// </summary>
		public LMSData.User User
		{
			get { return LMSData.User.FetchByID(this.UserID); }

			set { SetColumnValue("UserID", value.UserID); }

		}

		
		
		#endregion
		
		
		
		//no ManyToMany tables defined (0)
		
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(int varForumID,int? varParentID,string varThreadName,int varUserID,DateTime? varCreatedOn,bool? varIsDeleted,int? varDepth)
		{
			Thread item = new Thread();
			
			item.ForumID = varForumID;
			
			item.ParentID = varParentID;
			
			item.ThreadName = varThreadName;
			
			item.UserID = varUserID;
			
			item.CreatedOn = varCreatedOn;
			
			item.IsDeleted = varIsDeleted;
			
			item.Depth = varDepth;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(int varThreadID,int varForumID,int? varParentID,string varThreadName,int varUserID,DateTime? varCreatedOn,bool? varIsDeleted,int? varDepth)
		{
			Thread item = new Thread();
			
				item.ThreadID = varThreadID;
				
				item.ForumID = varForumID;
				
				item.ParentID = varParentID;
				
				item.ThreadName = varThreadName;
				
				item.UserID = varUserID;
				
				item.CreatedOn = varCreatedOn;
				
				item.IsDeleted = varIsDeleted;
				
				item.Depth = varDepth;
				
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		#endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string ThreadID = @"ThreadID";
			 public static string ForumID = @"ForumID";
			 public static string ParentID = @"ParentID";
			 public static string ThreadName = @"ThreadName";
			 public static string UserID = @"UserID";
			 public static string CreatedOn = @"CreatedOn";
			 public static string IsDeleted = @"IsDeleted";
			 public static string Depth = @"Depth";
						
		}

		#endregion
	}

}

